# 来源 https://pymupdf.readthedocs.io/en/latest/pymupdf4llm/index.html
# pip install pymupdf4llm
# convert the document to markdown


import pymupdf4llm
from pathlib import Path
from ..file_processor import FileProcessor

class PDFToMarkdownProcessor(FileProcessor):
    def _process_file(self):
        # 使用 pymupdf4llm 将 PDF 转换为 Markdown
        md_text = pymupdf4llm.to_markdown(self.input_filepath)

        # 在这里可以添加自定义的 PDF 处理逻辑
        # 例如：调整标题级别，处理特定的样式等

        # 将处理后的 Markdown 写入输出文件
        Path(self.output_filepath).write_bytes(md_text.encode('utf-8'))

def test(arg={}):
    # Usage example
    processor = PDFToMarkdownProcessor("input.pdf", "output/converted.md")
    result = processor.process()
    print(result)

if __name__ == "__main__":
    PDFToMarkdownProcessor.main()

"""
使用示例：
python3 -m document2llm.md.pdf2md.pdf_to_markdown -i input.pdf -o output/pdf_converted.md

或者在Python代码中：

from document2llm.md.pdf2md.pdf_to_markdown import PDFToMarkdownProcessor

processor = PDFToMarkdownProcessor("input.pdf", "output/converted-pdf.md")
result = processor.process()
print(result)
"""


#
